import 'dart:io';
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/material.dart';
import 'package:flutter_displaymode/flutter_displaymode.dart';
import 'package:provider/provider.dart';
import 'package:wallpaper/db/love_database.dart';
import 'package:wallpaper/generated/l10n.dart';
import 'package:wallpaper/http/dio_instance.dart';
import 'package:wallpaper/pages/navbar/child/setting_page.dart';
import 'package:wallpaper/pages/navbar/home_page.dart';
import 'package:wallpaper/pages/navbar/welcome_page.dart';
import 'package:wallpaper/pages/theme/theme_page.dart';
import 'package:wallpaper/providers/setting_provider.dart';
import 'package:wallpaper/proxy/my_http_overrides.dart';
import 'package:wallpaper/themes/localization_info.dart';
import 'package:wallpaper/themes/theme_provider.dart';
// ignore: depend_on_referenced_packages
import 'package:flutter_localizations/flutter_localizations.dart';

// 分包打包命令： flutter build apk --target-platform android-arm,android-arm64,android-x64 --split-per-abi
// 分包打包命令： flutter build apk --target-platform android-arm64 --split-per-abi
void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  // 设置最高刷新率
  await FlutterDisplayMode.setHighRefreshRate();

  // 初始化 note isar 数据库
  await LoveDatabase.initialize();
  // dio初始化
  DioInstance.instance().initDio(baseUrl: '');
  DioInstance.instance().addResponseInterceptor();

  PaintingBinding.instance.imageCache.maximumSize = 80; // 设置缓存数量
  PaintingBinding.instance.imageCache.maximumSizeBytes =
      80 << 20; // 设置缓存大小为 300 MB

  HttpOverrides.global = MyHttpOverrides();

  runApp(MultiProvider(
    providers: [
      ChangeNotifierProvider(create: (context) => ThemeProvider()),
      ChangeNotifierProvider(create: (contex) => SettingProvider()),
      ChangeNotifierProvider(create: (contex) => LoveDatabase()),
      ChangeNotifierProvider(create: (context) => LocalizationInfo()), // 新增
    ],
    child: MyApp(),
  ));
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return Consumer<LocalizationInfo>(builder: (context, provider, child) {
      return MaterialApp(
        debugShowCheckedModeBanner: false,
        locale: _getLocale(provider.localeKey),
        // 国际化支持
        localizationsDelegates: const [
          GlobalCupertinoLocalizations.delegate,
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          S.delegate
        ],
        supportedLocales: [
          // Locale('zh', 'CH'),
          Locale('en', 'US'),
          ...S.delegate.supportedLocales
        ],
        title: '波奇壁纸',
        theme: Provider.of<ThemeProvider>(context).themeData,
        builder: BotToastInit(), //1. call BotToastInit
        navigatorObservers: [
          BotToastNavigatorObserver()
        ], //2. registered route observer
        home: Provider.of<SettingProvider>(context).isCloseWelcomeBocchi
            ? HomePage()
            : WelcomePage(),
        routes: {
          '/setting': (context) => const SettingPage(),
          '/theme': (context) => const ThemePage(),
        },
      );
    });
  }

  Locale _getLocale(String localeKey) {
    switch (localeKey) {
      case 'zh_CN':
        return const Locale('zh', 'CN');
      case 'en':
        return const Locale('en');
      default:
        return const Locale('zh', 'CN');
    }
  }
}
